import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Nf from '../components/noFound.vue'
const routes = [
	{
		path: '/login',
		component: Login,
		meta: {
			title: "登录"
		},
	},
	{
		path: '/home',
		component: Home,
		meta: {
			title: "主页"
		},
		redirect: '/welcome',
		children: [{
			path: '/welcome',
			component:()=>import('../components/welcome.vue')
		},
		
		{
			path: '/member',
			component:()=>import('../views/member.vue')
		},

		{
			path: '/family',
			component:()=>import('../views/family.vue')
		},

		{
			path: "/relation",
			component:()=>import('../views/relation.vue')
		},

		{
			path: "/describe",
			component:()=>import('../views/describe.vue')
		},
		
		{
			path: '/tree',
			component:()=>import('../views/tree.vue')
		},
		
		],
	},

	{
		path: "/404",
		component: Nf,
		meta: {
			title: "404"
		},
	},

	{
		path: "/:catchAll(.*)",
		redirect: "/404"
	},
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})


router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title;
	}
	if (to.path == '/login') return next(); // 默认访问路径为登录
	const flagStr = window.sessionStorage.getItem("username");
	if (!flagStr) return next('/login');
	next();
})

export default router
